package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.common.RedisContentConstant;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.service.ContentService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
@DubboService
public class ContentServiceImpl implements ContentService {
    @Resource
    private TbContentMapper tbContentMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public List<TbContent> getContentAll() {
        return tbContentMapper.selectByExample(null);
    }

    @Override
    public int addContent(TbContent tbContent) {
        deleteKey();
        return tbContentMapper.insert(tbContent);
    }

    @Override
    public TbContent updateContentEcho(Long id) {
        deleteKey();
        return tbContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateContent(TbContent tbContent) {
        deleteKey();
        return tbContentMapper.updateByPrimaryKeySelective(tbContent);
    }

    @Override
    public int deleteContent(Long id) {
        deleteKey();
        return tbContentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateOnOperationContent(Long[] ids) {
        int i =tbContentMapper.updateOnOperationContent(ids);
        return i;
    }

    @Override
    public int updateCloseOperationContent(Long[] ids) {
        int i =tbContentMapper.updateCloseOperationContent(ids);
        return i;
    }
    //    删除多个key
    public void deleteKey (){
        redisTemplate.delete(RedisContentConstant.CONTENT_ADVERTISEMENT+":*");
    }
    @Override
    public List<TbContent> selectIndexPageAdvertisement(Integer pageSize, Long categoryId) {
        BoundZSetOperations<String, String> str = redisTemplate.boundZSetOps(RedisContentConstant.CONTENT_ADVERTISEMENT + ":" + categoryId);
//        str.range(0,-1).forEach(v -> System.out.println("集合中的值:" + v));
        Set set = str.range(0, -1);
        System.out.println(set);
        if (set.size() != 0){
            List<TbContent> tbContents = new ArrayList<>();
            System.out.println(set);
            for (Object o : set) {
                System.out.println(o);
                System.out.println(o.toString());
            }
            str.range(0,-1).forEach(v-> tbContents.add(JSONObject.parseObject(v,TbContent.class)));
            return tbContents;
        }else {
            List<TbContent> tbContents = tbContentMapper.selectIndexPageAdvertisement(pageSize, categoryId);
            for (TbContent tbContent : tbContents) {
                String toJSONString = JSONObject.toJSONString(tbContent);
                str.add(toJSONString,tbContent.getId());
            }
            return tbContents;
        }


    }
}
